package com.xunyun.kotlinwc.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.xunyun.kotlinwc.widget.LoadingDialog

/**
 * @author gl
 * @date 2021/9/7
 * @desc
 */
abstract class BaseActivity<T : ViewDataBinding, VM : BaseViewModel> : AppCompatActivity() {

    protected lateinit var mBinding: T
    protected lateinit var mViewModel: VM
    protected lateinit var loadingDialog: LoadingDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this, getLayoutId());
        mViewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())
            .get(getViewModel()::class.java);
        mBinding.lifecycleOwner = this;
        bindViewModel();
        loadingDialog = LoadingDialog(this)
        initView(savedInstanceState)
        initData()
    }

    abstract fun getLayoutId(): Int
    abstract fun getViewModel(): VM;

    /**
     * 绑定 ViewModel
     */
    abstract fun bindViewModel()
    abstract fun initView(savedInstanceState: Bundle?)
    abstract fun initData()

}